<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>树统计</title>
    <script>
        const assetsTypeTree = [{
            name: "ETF",
            code: "1",
            children: [{ name: "板块", code: "1", risk: '4' }, { name: "股债混合", code: "2", risk: '3' },
            { name: "债券", code: "3", risk: '2' }, { name: "黄金", code: "4", risk: '2' },
            { name: "白银", code: "5", risk: '2' }, { name: "指数", code: "7", risk: '4' },
            { name: "货币", code: "8", risk: '2' }
            ]
        },
        {
            name: "股票",
            code: "2",
            children: [{ name: "红利股", code: "1", risk: '5' }, { name: "长期持有", code: "2", risk: '6' }, { name: "缠论操作", code: "3", risk: '6' }, { name: "少妇战法", code: "3", risk: '6' }]
        },
        {
            name: "存款",
            code: "3",
            children: [{ name: "活期", code: "1", risk: '1' }, { name: "定期", code: "2", risk: '1' }]
        },
        { name: "债券", code: "4", children: [{ name: "国债", code: "1", risk: '1' }, { name: "地方债", code: "2", risk: '1' }, { name: "公司债", code: "3", risk: '1' }] },
        { name: "实物黄金", code: "5", risk: '0' }
        ]

        const items = [{ assetsType: '1-1', money: 100, channel: '1', remark: 'ETF-半导体板块' }, { assetsType: '1-1', money: 200, channel: '1', remark: 'ETF-软件板块' },
        { assetsType: '1-1', money: 300, channel: '1', remark: 'ETF-军工板块' }, { assetsType: '1-1', money: 300, channel: '1', remark: 'ETF-光伏板块' },
        { assetsType: '1-4', money: 400, channel: '1', remark: 'ETF-黄金-博时' }, { assetsType: '1-4', money: 400, channel: '1', remark: 'ETF-黄金-国泰' },
        { assetsType: '1-7', money: 400, channel: '1', remark: 'ETF-指数-沪深300' }, { assetsType: '1-7', money: 400, channel: '1', remark: 'ETF-指数-科创100' },
        { assetsType: '5', money: 5000, channel: '1', remark: '实物黄金' },
        { assetsType: '3-1', money: 5000, channel: '1', remark: '支付宝-活期' }, { assetsType: '3-1', money: 5000, channel: '1', remark: '证券-活期' },
        { assetsType: '2-1', money: 5000, channel: '1', remark: '股票-红利股-电力' }, { assetsType: '2-1', money: 5000, channel: '1', remark: '红利股-有色' },
        { assetsType: '2-1', money: 5000, channel: '1', remark: '股票-红利股-制造业' }, { assetsType: '2-2', money: 5000, channel: '1', remark: '消费-中免' },
        { assetsType: '2-3', money: 5000, channel: '1', remark: '股票-短期操作-人民网' }
        ];

        function findNode(tree, typeArr, level) {
            for (let i = 0; i < tree.length; i++) {
                const node = tree[i];
                if (node.code == typeArr[level]) {
                    return tree[i];
                }
            }
            return null;
        }
        function addData(tree, item) {
            const type = item['assetsType'];
            const typeArr = type.split('-');
            let current = null;
            let treeNodeArr = tree;
            for (let i = 0; i < typeArr.length; i++) {
                current = findNode(treeNodeArr, typeArr, i);
                if(current == null){
                    return null;
                }
                // 统计金额,数量
                if (current.money) {
                    current.count++
                    current.money = current.money + item.money
                } else {
                    current.money = item.money
                    current.count = 1
                }

                if ((typeArr.length - 1) == i) {
                    // 判断是否是最后一个节点
                    if (current.data) {
                        current.data.push(item)
                    } else {
                        current.data = [item]
                    }
                } else {
                    treeNodeArr = current.children
                }

            }
        }

        for (let i = 0; i < items.length; i++) {
            const item = items[i];
            // const node = findNode(assetsTypeTree,item);
            addData(assetsTypeTree, item);
        }
        console.log(assetsTypeTree)

    </script>
</head>

<body>

</body>

</html>